/************************************************************************
 * File: button.cpp                                                     *
 *                                                                      *
 * Copyright 2006, 2008 Colin Kern (colin.kern@gmail.com)               *
 *                                                                      *
 * This file is part of Profit.  Some previous versions were released   *
 * under the names Megacorp Mania and Acquire.                          *
 *                                                                      *
 * Profit is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation, either version 3 of the License, or    *
 * any later version.                                                   *
 *                                                                      *
 * Profit is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with Profit.  If not, see <http://www.gnu.org/licenses/>.      *
 ************************************************************************/
#include "button.h"
#include <QString>
#include <QPainter>
#include <QMouseEvent>

Button::Button(QWidget *parent) : QWidget(parent)
{
	enable();
	setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}

Button::Button(const QString &text, QWidget *parent) : QWidget(parent)
{
	setText(text);
}

QSize Button::sizeHint() const
{
	return QSize(100, 30);
}

bool Button::enabled() const
{
	return m_enabled;
}

void Button::setText(const QString &text)
{
	m_text = text;
}

void Button::enable()
{
	m_enabled = true;
	update();
	emit hoverChanged(m_hover);
}

void Button::disable()
{
	m_enabled = false;
	update();
}

void Button::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	
	QRadialGradient gradient(rect().center(), rect().width(), rect().center());
	
	if (m_enabled) {
		if (m_hover) {
			gradient.setColorAt(0.0, QColor(96, 196, 255));
			gradient.setColorAt(1.0, Qt::gray);
		} else {
			gradient.setColorAt(0.0, Qt::darkGray);
			gradient.setColorAt(1.0, QColor(96, 96, 96));
		}
	} else {
		gradient.setColorAt(0.0, QColor(32, 32, 32));
		gradient.setColorAt(1.0, QColor(32, 32, 32));
	}
	
	painter.setBrush(QBrush(gradient));
	painter.setPen(Qt::NoPen);
	painter.drawRoundedRect(rect(), 10, 10);
	
	painter.setPen(Qt::black);
	painter.setFont(QFont("Helvetica", 12, QFont::Bold));
	painter.drawText(rect(), Qt::AlignHCenter | Qt::AlignVCenter, m_text);
}

void Button::mousePressEvent(QMouseEvent *event)
{
	if (m_enabled && event->button() == Qt::LeftButton) {
		emit clicked();
	}
}

void Button::enterEvent(QEvent *event)
{
	m_hover = true;
	update();
	emit hoverChanged(m_hover);
}

void Button::leaveEvent(QEvent *event)
{
	m_hover = false;
	update();
	emit hoverChanged(m_hover);
}
	